package mx.netbrain.controller;

import java.util.Date;
import java.util.List;

import mx.netbrain.dao.PropertiesRepositoriesDao;
import mx.netbrain.dao.impl.PropertiesRepositoriesDaoImpl;
import mx.netbrain.model.PropertiesRepositories;
import mx.netbrain.model.PropertiesRepositoriesValue;
import mx.netbrain.model.PropertiesRepositoriesValueId;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.Status;
import mx.netbrain.model.Tree;
import mx.netbrain.model.TypeField;

public class PropertiesController {

	public static void saveProperties(String[] props,
			List<PropertiesRepositories> listProperties, Tree tree) {
		for (int i = 0; i < props.length; i++) {
			PropertiesRepositoriesDao propertiesDao = new PropertiesRepositoriesDaoImpl();
			PropertiesRepositoriesValue propertiesValue = new PropertiesRepositoriesValue();
			PropertiesRepositoriesValueId propertiesValueId = new PropertiesRepositoriesValueId();
			propertiesValueId.setProperties(listProperties.get(i).getId());
			propertiesValueId.setRepository(tree.getRepositories().getId());
			propertiesValueId.setTree(tree.getId());
			propertiesValue.setId(propertiesValueId);
			propertiesValue.setValue(props[i]);
			propertiesDao.saveValue(propertiesValue);
		}
	}

	public static void addProperties(String label, Boolean mandatory,
			TypeField typeField, Repositories repository, Integer size) {
		PropertiesRepositories propertiesRepositories = new PropertiesRepositories();
		PropertiesRepositoriesDao propertiesRepositoriesDao = new PropertiesRepositoriesDaoImpl();
		Status status = new Status();
		status.setId(1);
		propertiesRepositories.setLabel(label);
		propertiesRepositories.setMandatory(mandatory);
		propertiesRepositories.setTypeField(typeField);
		propertiesRepositories.setRepositories(repository);
		propertiesRepositories.setSize(size);
		propertiesRepositories.setRegistry(new Date());
		propertiesRepositories.setName(label);
		propertiesRepositories.setStatus(status);
		propertiesRepositoriesDao.save(propertiesRepositories);
	}

	public static List<PropertiesRepositories> listForRepositories(
			Repositories repository) {
		PropertiesRepositoriesDao propertiesRepositoriesDao = new PropertiesRepositoriesDaoImpl();
		return propertiesRepositoriesDao.listForRepositories(repository);
	}

}
